///
//  HighQuViewController.m
//  Qihuo
//
//  Created by Vencoo on 13-11-15.
//  Copyright (c) 2013年 Vencoo. All rights reserved.
//
#import "secureXJViewController.h"
#import "HighQuViewController.h"
#import "JSONKit.h"
@interface HighQuViewController ()
{
//    ASIFormDataRequest *obtainInfo;
    ASIFormDataRequest *sendRequest;
    
    UIImage *roundSelect;
    UIImage *roundUnselect;
    
    NSArray *queryListArr;
    
    NSMutableArray *checkNameLBs;
    NSMutableArray *childNameLBs;
    NSMutableArray *remarkTFs;
    NSMutableArray *selectBtnArr;
    NSMutableArray *remarkLBs;
    NSMutableArray *isNormals;
    NSMutableArray *remarks;
    NSMutableArray *checkIds;
    NSMutableArray *faultCheckId;
    int tableCount1;
    int tableCount2;
}
@end

@implementation HighQuViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    NSArray *queryDataArr = [NSArray array];
    switch (self.delegate.HighQu) {
        case 0:
        {
            self.title = @"高区+外围巡检";
            self.sendUrl=[NSString stringWithFormat:addHighQu];
            self.checkXunJianArea = @"1";
            queryDataArr = @[@{@"floorId": @"1",@"floorName":@"37F",@"frid":@"<#string#>"},@{@"floorId": @"2",@"floorName":@"36F",@"frid":@"<#string#>"},@{@"floorId": @"3",@"floorName":@"35F",@"frid":@"<#string#>"},@{@"floorId": @"4",@"floorName":@"34F",@"frid":@"<#string#>"},@{@"floorId": @"5",@"floorName":@"33F",@"frid":@"<#string#>"},@{@"floorId": @"6",@"floorName":@"32F",@"frid":@"<#string#>"},@{@"floorId": @"7",@"floorName":@"31F",@"frid":@"<#string#>"},@{@"floorId": @"8",@"floorName":@"30F",@"frid":@"<#string#>"},@{@"floorId": @"9",@"floorName":@"29F",@"frid":@"<#string#>"},@{@"floorId": @"10",@"floorName":@"28F",@"frid":@"<#string#>"},@{@"floorId": @"11",@"floorName":@"27F",@"frid":@"<#string#>"},@{@"floorId": @"12",@"floorName":@"26F",@"frid":@"<#string#>"},@{@"floorId": @"13",@"floorName":@"25F",@"frid":@"<#string#>"},@{@"floorId": @"14",@"floorName":@"24F",@"frid":@"<#string#>"},@{@"floorId": @"15",@"floorName":@"23F",@"frid":@"<#string#>"},@{@"floorId": @"16",@"floorName":@"22F",@"frid":@"<#string#>"},@{@"floorId": @"17",@"floorName":@"21F",@"frid":@"<#string#>"},@{@"floorId": @"18",@"floorName":@"20F",@"frid":@"<#string#>"},@{@"floorId": @"19",@"floorName":@"19F",@"frid":@"<#string#>"},@{@"floorId": @"20",@"floorName":@"18F",@"frid":@"<#string#>"},@{@"floorId": @"21",@"floorName":@"17F",@"frid":@"<#string#>"},@{@"floorId": @"22",@"floorName":@"16F",@"frid":@"<#string#>"},@{@"floorId": @"23",@"floorName":@"15F",@"frid":@"<#string#>"},@{@"floorId": @"24",@"floorName":@"14F",@"frid":@"<#string#>"},@{@"floorId": @"25",@"floorName":@"13F",@"frid":@"<#string#>"},@{@"floorId": @"26",@"floorName":@"12F",@"frid":@"<#string#>"},@{@"floorId": @"27",@"floorName":@"11F",@"frid":@"<#string#>"},@{@"floorId": @"28",@"floorName":@"10F",@"frid":@"<#string#>"},@{@"floorId": @"29",@"floorName":@"9F",@"frid":@"<#string#>"},@{@"floorId": @"30",@"floorName":@"外围东",@"frid":@"<#string#>"},@{@"floorId": @"31",@"floorName":@"外围南",@"frid":@"<#string#>"},@{@"floorId": @"32",@"floorName":@"外围西",@"frid":@"<#string#>"},@{@"floorId": @"33",@"floorName":@"外围北",@"frid":@"<#string#>"}];
        }
            break;
        case 1:
        {
            self.title = @"低区+外围巡检";
            self.checkXunJianArea = @"2";
            self.sendUrl=[NSString stringWithFormat:addCheckLowResult];
            queryDataArr = @[@{@"floorId": @"1",@"floorName":@"8F",@"frid":@"<#string#>"},@{@"floorId": @"2",@"floorName":@"7F",@"frid":@"<#string#>"},@{@"floorId": @"3",@"floorName":@"6F",@"frid":@"<#string#>"},@{@"floorId": @"4",@"floorName":@"5F",@"frid":@"<#string#>"},@{@"floorId": @"5",@"floorName":@"4F",@"frid":@"<#string#>"},@{@"floorId": @"6",@"floorName":@"3F",@"frid":@"<#string#>"},@{@"floorId": @"7",@"floorName":@"2F",@"frid":@"<#string#>"},@{@"floorId": @"8",@"floorName":@"1F",@"frid":@"<#string#>"},@{@"floorId": @"9",@"floorName":@"裙6F",@"frid":@"<#string#>"},@{@"floorId": @"10",@"floorName":@"裙5F",@"frid":@"<#string#>"},@{@"floorId": @"11",@"floorName":@"裙4F",@"frid":@"<#string#>"},@{@"floorId": @"12",@"floorName":@"裙3F",@"frid":@"<#string#>"},@{@"floorId": @"13",@"floorName":@"裙2F",@"frid":@"<#string#>"},@{@"floorId": @"14",@"floorName":@"裙1F",@"frid":@"<#string#>"},@{@"floorId": @"15",@"floorName":@"车库B1",@"frid":@"<#string#>"},@{@"floorId": @"16",@"floorName":@"车库B2",@"frid":@"<#string#>"},@{@"floorId": @"17",@"floorName":@"车库B3",@"frid":@"<#string#>"},@{@"floorId": @"18",@"floorName":@"外围东",@"frid":@"<#string#>"},@{@"floorId": @"19",@"floorName":@"外围南",@"frid":@"<#string#>"},@{@"floorId": @"20",@"floorName":@"外围西",@"frid":@"<#string#>"},@{@"floorId": @"21",@"floorName":@"外围北",@"frid":@"<#string#>"}];
        }
        default:
            break;
    }
    [super viewDidLoad];
    self.floorNames = [NSMutableArray array];
    [self.floorIds removeAllObjects];
    for (NSDictionary *dic in queryDataArr)
    {
        [self.floorNames addObject:[dic valueForKey:@"floorName"]];
        [self.floorIds addObject:[dic valueForKey:@"floorId"]];
        [self.sendFloorDatas addObject:@""];
        [self.xunJianCellInfos addObject:@{@"hidden":@"1",@"floorId":[dic valueForKey:@"floorId"] }];
    }
    
    [self initData];
    [self showUIWithArray:self.allShowDataArr andFloorId:@"1"];
    NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.myTableView selectRowAtIndexPath:index animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}
#pragma mark - initilize
- (void)initData
{
    roundSelect = [UIImage imageNamed:@"选中"];
    roundUnselect = [UIImage imageNamed:@"未选中"];
    
    remarks = [NSMutableArray array];
    checkIds = [NSMutableArray array];
    checkNameLBs = [NSMutableArray array];
    childNameLBs = [NSMutableArray array];
    remarkTFs = [NSMutableArray array];
    selectBtnArr = [NSMutableArray array];
    remarkLBs = [NSMutableArray array];
    isNormals = [NSMutableArray array];
    self.sendCheckDatas = [NSMutableArray array];

    NSDictionary *firstDic = [NSDictionary dictionaryWithObjectsAndKeys:@"一、门禁：",@"checkName",@[@"1、门禁设施完好",@"2、门禁设施无污染"],@"childName",@"1",@"checkId", nil];
    NSDictionary *secondDic = [NSDictionary dictionaryWithObjectsAndKeys:@"二、消防通道：",@"checkName",@[@"1、无堵塞",@"2、门禁完好",@"3、通道内照明",@"4、消防前室内设备完好",@"5、无异味"],@"childName",@"2",@"checkId", nil];
    NSDictionary *thirdDic = [NSDictionary dictionaryWithObjectsAndKeys:@"三、关门情况：",@"checkName",@[@"1、单元门虚关、未关",@"2、门锁有故障"],@"childName",@"3",@"checkId", nil];
    NSDictionary *forthDic = [NSDictionary dictionaryWithObjectsAndKeys:@"四、施工情况：",@"checkName",@[@"1、施工人员持证操作",@"2、施工现场有无安全隐患"],@"childName",@"4",@"checkId", nil];
    NSDictionary *fifthDic = [NSDictionary dictionaryWithObjectsAndKeys:@"五、公共区域照明：",@"checkName",@[@"1、灯具完好",@"2、灯无闪烁"],@"childName",@"5",@"checkId", nil];
    NSDictionary *sixDic = [NSDictionary dictionaryWithObjectsAndKeys:@"六、公共区域设备：",@"checkName",@[@"1、卫生间水斗、坐便器出水正常",@"2、工作水斗有无漏水现象",@"3、有无异响",@"4、有无异味",@"5、墙面有无损坏"],@"childName",@"6",@"checkId", nil];
    NSDictionary *sevenDic = [NSDictionary dictionaryWithObjectsAndKeys:@"七、公共区域清洁：",@"checkName",@[@"1、有无污渍",@"2、有无水渍",@"3、有无垃圾"],@"childName",@"7",@"checkId", nil];
    NSDictionary *eightDic = [NSDictionary dictionaryWithObjectsAndKeys:@"八、人员情况：",@"checkName",@[@"1、单元人员加班情况",@"2、是否有可疑人员"],@"childName",@"8",@"checkId", nil];
    self.allShowDataArr = @[firstDic,secondDic,thirdDic,forthDic,fifthDic,sixDic,sevenDic,eightDic];
}
- (void)showUIWithArray:(NSArray *)array andFloorId:(NSString *)floorId
{
    [self clearAllControl];
    self.floorIdQuanju = floorId;
    id receiveFloorObj = [self.sendFloorDatas objectAtIndex:[self.floorIds indexOfObjectss:self.floorIdQuanju]];
    if (![receiveFloorObj isKindOfClass:[NSString class]])
    {
        self.sendCheckDatas = [receiveFloorObj objectForKey:@"checkData"];
    }
    else
    {
        self.sendCheckDatas  = [NSMutableArray array];
        for (int o = 0; o <array.count; o ++)
        {
            [self.sendCheckDatas addObject:@{@"checkId": [NSString stringWithFormat:@"%d",o+1],@"isNormal":@"3",@"checkRemark":@""}];
        }
    }
    for (int o = 0; o <array.count; o ++)
    {
        [checkIds addObject:[NSString stringWithFormat:@"%d",o+1]];
    }
    faultCheckId = [NSMutableArray array];
    if ([floorId integerValue] > self.floorIds.count - 4)
    {
        NSMutableIndexSet *indexes = [NSMutableIndexSet indexSet];
        for (int j = 0; j < array.count; j ++) {
            if (j != 1)
            {
                [indexes addIndex:j];
            }
        }
        array = [array objectsAtIndexes:indexes];
    }
    
    for (int j = 0; j < array.count; j++)
    {
        NSDictionary *dic = [array objectAtIndex:j];
        NSArray *childNames = [dic objectForKey:@"childName"];
        [faultCheckId addObject:[dic objectForKey:@"checkId"]];
//        添加标题
        UILabel *checkNameLb = [UILabel new];
        checkNameLb.text = [dic objectForKey:@"checkName"];
        checkNameLb.font = [UIFont systemFontOfSize:28];
        CGSize mySz = [checkNameLb.text sizeWithFont:checkNameLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
        checkNameLb.frame = CGRectMake(10, [[remarkTFs lastObject]frame].origin.y + [[remarkTFs lastObject]frame].size.height + interval111, mySz.width, mySz.height);
        [self.backScrollV addSubview:checkNameLb];
        [checkNameLBs addObject:checkNameLb];
        [self.controlWillClears addObject:checkNameLb];
        for (int k = 0; k < childNames.count; k++)
        {
            UILabel *childNameLb = [UILabel new];
            childNameLb.text = [childNames objectAtIndex:k];
            childNameLb.font = [UIFont systemFontOfSize:20];
            mySz = [childNameLb.text sizeWithFont:childNameLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
            childNameLb.frame = CGRectMake(checkNameLb.frame.origin.x + interval111 * 2, checkNameLb.frame.origin.y + (mySz.height + interval111)*(k+1), mySz.width, mySz.height);
            [self.backScrollV addSubview:childNameLb];
            [childNameLBs addObject:childNameLb];
            [self.controlWillClears addObject:childNameLb];
        }
        [self addSingleSelect:self.abnormals andCheckId:[checkIds objectAtIndex:j]];
        [isNormals addObject:@""];
        [remarks addObject:@""];
    }
    if (![receiveFloorObj isKindOfClass:[NSString class]])
    {
        [self addCommentKuang:[receiveFloorObj objectForKey:@"floorRemark"]];
    }
    else
    {
        [self addCommentKuang:@""];
    }
}

- (void)addSingleSelect:(NSArray*)options andCheckId:(NSString *)checkId
{
    NSMutableArray *btnArr = [NSMutableArray array];
    id receiveFloorObj = [self.sendFloorDatas objectAtIndex:[self.floorIds indexOfObjectss:self.floorIdQuanju]];
    NSArray *receiveCheckArr = [NSArray array];
    NSString *receiveCheckId = @"";
    NSString *receiveIsnormal = @"3";
    NSString *receiveCheckRemark = @"";
    if (![receiveFloorObj isKindOfClass:[NSString class]])
    {
        receiveCheckArr = [receiveFloorObj objectForKey:@"checkData"];
        NSDictionary *receiveCheckDic = [receiveCheckArr objectAtIndex:[checkIds indexOfObjectss:checkId]];
        receiveCheckId = [receiveCheckDic objectForKey:@"checkId"];
        receiveIsnormal = [receiveCheckDic objectForKey:@"isNormal"];
        receiveCheckRemark = [receiveCheckDic objectForKey:@"checkRemark"];
    }
    for (int j =0; j<options.count; j++)
    {
        UIButton *selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [selectBtn setImage:roundUnselect forState:UIControlStateNormal];
        if (j == [receiveIsnormal integerValue])
        {
            [selectBtn setImage:roundSelect forState:UIControlStateNormal];
        }
        [selectBtn addTarget:self action:@selector(selectOptions:) forControlEvents:UIControlEventTouchUpInside];
        selectBtn.frame = CGRectMake(62 + (self.backScrollV.frame.size.width - 62)/2*j, [[childNameLBs lastObject]frame].origin.y + ([[childNameLBs lastObject]frame].size.height + interval111), roundSelect.size.width, roundSelect.size.height);
        [self.backScrollV addSubview:selectBtn];
        [btnArr addObject:selectBtn];
        [self.controlWillClears addObject:selectBtn];
        
        UILabel *abnormLb = [UILabel new];
        NSString *abnormStr = [options objectAtIndex:j];
        abnormLb.font = [UIFont systemFontOfSize:26];
        abnormLb.text = abnormStr;
        CGSize mySize = [abnormLb.text sizeWithFont:abnormLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
        abnormLb.frame = CGRectMake(selectBtn.frame.origin.x + selectBtn.frame.size.width, selectBtn.frame.origin.y, mySize.width, mySize.height);
        [self.backScrollV addSubview:abnormLb];
        [self.controlWillClears addObject:abnormLb];
    }
    UILabel *remarkLb = [UILabel new];
    remarkLb.text = @"备注：";
    remarkLb.font = [UIFont systemFontOfSize:25];
    CGSize mySiz = [remarkLb.text sizeWithFont:remarkLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    remarkLb.frame = CGRectMake(50, [[childNameLBs lastObject]frame].origin.y + ([[childNameLBs lastObject]frame].size.height + interval111)*2, mySiz.width, mySiz.height);
    [self.backScrollV addSubview:remarkLb];
    [self.controlWillClears addObject:remarkLb];
    
    UITextField *remarkTf = [[UITextField alloc]initWithFrame:CGRectMake(remarkLb.frame.origin.x + remarkLb.frame.size.width, remarkLb.frame.origin.y, 300, mySiz.height)];
    remarkTf.text = [receiveCheckRemark stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    remarkTf.background = [UIImage imageNamed:@"输入框底"];
    remarkTf.delegate = self;
    [self.backScrollV addSubview:remarkTf];
    [remarkTFs addObject:remarkTf];
    [selectBtnArr addObject:btnArr];
    self.backScrollV.contentSize = CGSizeMake(self.backScrollV.frame.size.width, remarkTf.frame.size.height + remarkTf.frame.origin.y + interval111);
    [self.controlWillClears addObject:remarkTf];
//    默认选中第一行
}

- (void)clearAllControl
{
    for (id label in checkNameLBs) {
        [label removeFromSuperview];
    }
    [checkNameLBs removeAllObjects];
    for (id label in childNameLBs) {
        [label removeFromSuperview];
    }
    [childNameLBs removeAllObjects];
    for (id label in remarkTFs) {
        [label removeFromSuperview];
    }
    [remarkTFs removeAllObjects];
    for (NSArray *btnArr in selectBtnArr)
    {
        for (id btn in btnArr)
        {
            [btn removeFromSuperview];
        }
    }
    for (id contr in self.controlWillClears) {
        [contr removeFromSuperview];
    }
    [selectBtnArr removeAllObjects];
    [isNormals removeAllObjects];
    [remarks removeAllObjects];
    [checkIds removeAllObjects];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.myTableView) {
        return self.floorNames.count;
    }else{
        for (int j = 0; j<self.tables.count; j++)
        {
            if (tableView == [self.tables objectAtIndex:j])
            {
                return [[self.tableFactors objectAtIndex:j]count];
            }
        }
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [super tableView:tableView cellForRowAtIndexPath:indexPath];
    static NSString *identifier = @"myCell";
    if (tableView == self.myTableView)
    {
        self.mycell.textLabel.text = [self.floorNames objectAtIndex:indexPath.row];
        return self.mycell;
    }
    else
    {
        for (int j = 0; j<self.tables.count; j++)
        {
            if (tableView == [self.tables objectAtIndex:j])
            {
                UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
                if (!cell)
                {
                    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
                }
                cell.textLabel.text = [[self.tableValues objectAtIndex:j]objectAtIndex:indexPath.row];
                return cell;
            }
        }
    }
    return nil;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [super tableView:tableView didSelectRowAtIndexPath:indexPath];
    [self hiddenAll];
    if (tableView == self.myTableView)
    {
        [UIView animateWithDuration:.9 animations:^
        {
            self.backScrollV.contentSize = CGSizeMake(0, 0);
        }];

        [self showUIWithArray:self.allShowDataArr andFloorId:[self.floorIds objectAtIndex:indexPath.row]];
        self.floorIdQuanju = [self.floorIds objectAtIndex:indexPath.row];
   
    }
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    for (int k = 0; k < remarkTFs.count; k ++)
    {
        id tf = [remarkTFs objectAtIndex:k];
        BOOL isNormal = [[isNormals objectAtIndex:k]integerValue];
        if (isNormal == YES && tf == textField)
        {
            return YES;
        }
    }
    return NO;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    for (int k = 0; k < remarkTFs.count; k ++)
    {
        UITextField *tf = [remarkTFs objectAtIndex:k];
        if (tf == textField) {
//            [remarks replaceObjectAtIndex:[checkIds indexOfObject:[faultCheckId objectAtIndex:k]] withObject:[textField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
            NSString *string = [textField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[faultCheckId objectAtIndex:k],@"checkId",@"1",@"isNormal",string,@"checkRemark",nil];
            [self.sendCheckDatas replaceObjectAtIndex:[checkIds indexOfObject:[faultCheckId objectAtIndex:k]] withObject:dic];
        }
    }
}

#pragma mark - button action

- (void)selectOptions:(id)sender
{
    UIButton *btn = (UIButton*)sender;
    for (int a = 0;a<selectBtnArr.count;a++)
    {
        NSArray *btnArr = [selectBtnArr objectAtIndex:a];
        if ([btnArr containsObject:btn])
        {
            for (int j = 0;j<btnArr.count;j++)
            {
                UIButton *seleBtn = [btnArr objectAtIndex:j];
                [seleBtn setImage:roundUnselect forState:UIControlStateNormal];
                if (btn == [btnArr objectAtIndex:j])
                {
                    [seleBtn setImage:roundSelect forState:UIControlStateNormal];
                    [isNormals replaceObjectAtIndex:a withObject:[NSString stringWithFormat:@"%d",j]];
                    if (j == 0)
                    {
                        [[[UIApplication sharedApplication]keyWindow]endEditing:YES];
                        UITextField *tf =[remarkTFs objectAtIndex:a];
                        tf.text = @"";
                    }
                    if (j == 1)
                    {
                        UITextField *tf =[remarkTFs objectAtIndex:a];
                        [tf becomeFirstResponder];
                    }
                    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[faultCheckId objectAtIndex:a],@"checkId",[NSString stringWithFormat:@"%d",j],@"isNormal",@"",@"checkRemark",nil];
                    [self.sendCheckDatas replaceObjectAtIndex:[checkIds indexOfObject:[faultCheckId objectAtIndex:a]] withObject:dic];
                }
            }
        }
    }
}
#pragma mark - end
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
